/*
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.resource.impl;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import ca.ioniq.server.resource.PromotionResource;
import ca.ioniq.server.service.MarketingService;
import ca.ioniq.server.service.exception.PromotionException;
import ca.ioniq.shared.entity.Item;
import ca.ioniq.shared.entity.Promotion;

public class PromotionResourceImpl implements PromotionResource {

    private MarketingService marketingService;

    @Inject
    public PromotionResourceImpl(MarketingService marketingService) {
        this.marketingService = marketingService;
    }
    
	public void addNewItemsToPromotion(Long promotionId, List<Item> items) {
		try {
			marketingService.addNewItemsToPromotion(promotionId, items);
		} catch (PromotionException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}		
	}

	public void delete(Long id) {
		try {
			marketingService.removePromotion(id);
		} catch (PromotionException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}	
	}

	public Promotion get(Long id) {
		return marketingService.viewPromotion(id);
	}

	public Promotion put(Promotion promotion) {
		return marketingService.createPromotion(promotion);
	}

	public void update(Promotion promotion) {
		try {
			marketingService.updatePromotion(promotion);
		} catch (PromotionException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}		
	}
}
